export const getNews = (data,ids) => {
    return {
        type:"GETNEWSCOM",
        payload:{
            data:data,
            ids:ids
        }
    }
};

export const getComments = (data) => {
    return {
        type:"GETCOMMENTS",
        payload:{
            data:data,
        }
    }
};

export const fetchNews = (ids) => {
    if(ids){
        var id ="id="+ids;
    }
    return (path) => {
        var url = `http://127.0.0.1:3000/news/list`;
        return fetch(url,{
            method:"POST",
            body:id,
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            },
        })
        .then(response => {
            return response.json();
        }) 
        .then(data => {
            path(getNews(data,ids))
        })
    }
};

export const fetchComments = (ids) => {
    if(ids){
        var id ="_id="+ids;
    }
    return (path) => {
        var url = `http://127.0.0.1:3000/commits/list`;
        return fetch(url,{
            method:"POST",
            body:id,
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            },
        })
        .then(response => {
            return response.json();
        }) 
        .then(data => {
            path(getComments(data))
        })
    }
}